package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.MenuDao;
import com.sixth.entity.Menu;
import com.sixth.entity.User;
import com.sixth.service.MenuService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import com.sixth.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 高振杨
 * @create: 2022-01-12 11:44
 **/
@Service(value = "menuServise")
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public CommonResult findByUserRid() {
        User user = (User) WebUtil.getSession().getAttribute("user");
        Integer byrid = menuDao.selectByrid(user.getUserid());
        List<Menu> list = menuDao.findMenuByUid(byrid);
        ArrayList<Menu> menus = new ArrayList<>();
        for (Menu menu : list) {
            if (menu.getPid()==0){
                menus.add(menu);
            }
        }
        for (Menu menu : list) {
            if (menu.getPid()==1){
                menus.add(menu);
            }
        }
        for (Menu menu1 : menus) {
            ArrayList<Menu> children = new ArrayList<>();
            for (Menu menu2 : list) {
                if (menu2.getPid().equals(menu1.getId())){
                    children.add(menu2);
                }
            }
            menu1.setChildren(children);
        }
        return new CommonResult(2000,"查询左侧菜单成功",menus);
    }

    @Override
    public CommonResult findAlling() {
        List<Menu> sysMenus = menuDao.selectAlling();

        return new CommonResult(2000,"查询成功",sysMenus);
    }

    @Override
    public CommonResult findAllMenu() {
        List<Menu> list = menuDao.findAllMenu();
        List<Menu> menus=new ArrayList<Menu>();//声明一个集合存放一级菜单

        //找到一级菜单
        for (Menu menu : list) {
            if (menu.getPid()==1){
                menus.add(menu);
            }
        }
        //找一级菜单对应的二级菜单
        for (Menu menu1 : menus) {
            ArrayList<Menu> children = new ArrayList<>();
            for (Menu menu2 : list) {
                if (menu2.getPid().equals(menu1.getId())){
                    children.add(menu2);
                }
            }
            menu1.setChildren(children);
        }
        return new CommonResult(2000,"查询所有菜单成功",menus);

    }

    @Override
    public TableLayUi findAll(int page, int limit, String title) {
        PageHelper.startPage(page,limit);
        List<Menu> allTitle = menuDao.findAllTitle(title);
        PageInfo<Menu> pageInfo = new PageInfo<>(allTitle);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult addMenu(Menu menu) {
        menuDao.insertSelective(menu);
        return null;
    }

    @Override
    public CommonResult queryMenuByPid(Integer id) {
        Integer integer = menuDao.queryMenuByPid(id);
        int i=integer;
        if (i>0){
            return new CommonResult(0,"有父级菜单",null);
        }
        return new CommonResult(5000,"有父级菜单",null);
    }

    @Override
    public CommonResult updateMenu(Menu menu) {
        int i = menuDao.updateByPrimaryKeySelective(menu);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult updatePermission(Integer roleid, String[] mids) {
        menuDao.deleteRoleMenu(roleid);
        Integer i = menuDao.insertPermission(roleid, mids);
        if (i > 0) {
            return CommonResult.success(null,"修改成功");
        }
        return CommonResult.fail("修改失败");
    }
    //获取默认被选中的菜单
    public CommonResult findRoleMenuId(Integer roleid) {
        List<Integer> menuById = menuDao.findRoleMenuId(roleid);
        System.out.println(menuById);
        return new CommonResult(2000,"查询成功",menuById);
    }
}
